package com.julis.activity.log;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.baseutils.File_Z;
import com.github.czy1121.view.RoundButton;
import com.julis.R;
import com.julis.activity.obu_function.OBU_Trade_Activity;
import com.julis.html_analyse.AppUtil;
import com.julis.thread.FileThread;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.leon.lfilepickerlibrary.LFilePicker;
import com.leon.lfilepickerlibrary.MyConfig;


import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RizhiActivity extends AppCompatActivity {
    @BindView(R.id.open)
    RoundButton open;
    @BindView(R.id.tv_path)
    TextView tvPath;
    @BindView(R.id.tv_tishi)
    TextView tv_tishi;

    @BindView(R.id.shuan)
    RoundButton shuan;

    FtpUtils ftpUtils;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file);
        ButterKnife.bind(this);
        File_Z.createFile(this);
        imei = AppUtil.getIMEI(this);
//        Log.e("test", "版本号：" + versionCode);
        tv_tishi.setText("本机INMEI：" + imei);
        tvPath.setText("当前上传文件路径：无");
        shuan.setVisibility(View.GONE);
        ftpUtils = new FtpUtils();

    }


    public void openFromActivity() {
        new LFilePicker()
                .withActivity(this)
                .withRequestCode(MyConfig.REQUESTCODE_FROM_ACTIVITY)
                .withTitle("文件选择")
                .withIconStyle(MyConfig.ICON_STYLE_BLUE)
                .withMutilyMode(false)
                .withMaxNum(2)
                .withStartPath("/storage/emulated/0/" + MyConfig.path)//指定初始显示路径
                .withNotFoundBooks("至少选择一个文件")
                .withIsGreater(false)//过滤文件大小 小于指定大小的文件
                .withFileSize(500 * 1024)//指定文件大小为500K
                .withChooseMode(false)//文件夹选择模式
                //.withFileFilter(new String[]{"txt", "png", "docx"})
                .start();
    }

    KProgressHUD dialog1;
    public void showdialog1() {
        dialog1 = KProgressHUD.create(RizhiActivity.this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel("正在上传中")
                .setCancellable(true)
                .setAnimationSpeed(2)
                .setDimAmount(0.5f)

                .show();
    }

    String path = null;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == MyConfig.REQUESTCODE_FROM_ACTIVITY) {
                List<String> list = data.getStringArrayListExtra(MyConfig.RESULT_INFO);
                path = data.getStringExtra("path");
//                Toast.makeText(getApplicationContext(), "选中的路径为" + path, Toast.LENGTH_SHORT).show();
                Log.i("LeonFilePicker", path);
                tvPath.setText("当前上传文件路径：" + path);
                shuan.setVisibility(View.VISIBLE);

            }
        }
    }
    String name;
    String imei;
    @OnClick({R.id.open, R.id.shuan, R.id.img_back })
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.open:
                openFromActivity();
                break;
            case R.id.img_back:
                 finish();
                break;
            case R.id.shuan:
                showdialog1();
                  imei = AppUtil.getIMEI(this);
                if (!TextUtils.isEmpty(path)) {
                   name = path.substring(path.lastIndexOf("/") + 1, path.length());
                   new FileThread(ftpUtils,imei,name,path,mHandler).start();

                } else {
                    Toast.makeText(this, "请选择需要上传的文件！", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @SuppressLint("HandlerLeak")
    Handler mHandler = new Handler() {

        //handleMessage为处理消息的方法
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {

                case 1:
                    if (dialog1!=null) {
                        dialog1.dismiss();
                    }


                    //不一定出來在哪个线程
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(RizhiActivity.this, "文件上传成功！", Toast.LENGTH_SHORT).show();

                        }
                    });

                    break;
                case 2:
                    if (dialog1!=null) {
                        dialog1.dismiss();
                    }


                    //不一定出來在哪个线程
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            Toast.makeText(RizhiActivity.this, "文件上传失败！", Toast.LENGTH_SHORT).show();

                        }
                    });

                    break;
                default:
                    break;
            }
            super.handleMessage(msg);
        }
    };

}
